//
//  ViewController.m
//  MixRN
//
//  Created by zsgjs on 2017/2/27.
//  Copyright © 2017年 zsgjs. All rights reserved.
//

#import "ViewController.h"
#import <React/RCTRootView.h>
#import "SecViewController.h"

@interface ViewController ()
@property (nonatomic, strong) RCTRootView *rootView;

@property (nonatomic, strong) UIView *topView;

@property (nonatomic, strong) UIButton *button;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];

    [self.view addSubview:self.topView];
    [self.topView addSubview:self.button];
    [self.view addSubview:self.rootView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = false;
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    self.rootView.frame = CGRectMake(0, self.view.bounds.size.height * 0.5, self.view.bounds.size.width, self.view.bounds.size.height * 0.5);
    
    self.topView.frame =  CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 0.5);
    
    self.button.frame = CGRectMake(self.view.bounds.size.width * 0.5 - 50, self.view.bounds.size.height * 0.25 - 17.5, 100, 35);
}

- (void)jump
{
    SecViewController *vc = [[SecViewController alloc] init];
//    vc.view = rootView;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - getter and setter
- (RCTRootView *)rootView
{
    
    
    
    if (!_rootView) {
        NSURL *jsCodeLocation;
        NSString * strUrl = @"http://127.0.0.1/index.ios.jsbundle?platform=ios&dev=true";
        
        jsCodeLocation = [NSURL URLWithString:strUrl];
        NSDate *currentDate = [NSDate date];
        NSTimeInterval currentTime = [currentDate timeIntervalSince1970];
        jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"index.ios" withExtension:@"jsbundle"];
        _rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                moduleName:@"ListDemo"
                                         initialProperties:nil
                                             launchOptions:nil];
        NSDate *completeDate = [NSDate date];
        NSTimeInterval completeTime = [completeDate timeIntervalSince1970];
        NSLog(@"excute time = %f", completeTime - currentTime);
    }
    return _rootView;
}

- (UIButton *)button
{
    if (!_button) {
        _button = [[UIButton alloc] init];
        [_button setTitle:@"jump" forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}

- (UIView *)topView
{
    if (!_topView) {
        _topView = [[UIView alloc] init];
        _topView.backgroundColor = [UIColor orangeColor];
    }
    return _topView;
}

@end
